package net.ideapad.struts.form;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.ideapad.data.User;
import net.ideapad.hibernate.HibernateSessionFactory;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.hibernate.Session;

/**
 * Java bean for the Manage Group action
 * 
 * MyEclipse Struts Creation date: 12-16-2009
 * 
 * XDoclet definition:
 * 
 * @struts.form name="manageGroupForm"
 */
public class ManageGroupForm extends ActionForm {
	// Fields
	private Integer groupID;
	private String groupName;
	private Integer addUserID;
	private Integer removeUserID;
	private Integer addModeratorID;
	private Integer removeModeratorID;

	private String addUserIDString;
	private String removeUserIDString;
	private String addModeratorIDString;
	private String removeModeratorIDString;

	// Getters and Setters
	public Integer getGroupID() {
		return groupID;
	}

	public void setGroupID(Integer groupID) {
		this.groupID = groupID;
	}

	public String getAddUserIDString() {
		return addUserIDString;
	}

	public void setAddUserIDString(String addUserIDString) {
		this.addUserIDString = addUserIDString;
	}

	public String getRemoveUserIDString() {
		return removeUserIDString;
	}

	public void setRemoveUserIDString(String removeUserIDString) {
		this.removeUserIDString = removeUserIDString;
	}

	public String getAddModeratorIDString() {
		return addModeratorIDString;
	}

	public void setAddModeratorIDString(String addModeratorIDString) {
		this.addModeratorIDString = addModeratorIDString;
	}

	public String getRemoveModeratorIDString() {
		return removeModeratorIDString;
	}

	public void setRemoveModeratorIDString(String removeModeratorIDString) {
		this.removeModeratorIDString = removeModeratorIDString;
	}

	public Integer getAddUserID() {
		return addUserID;
	}

	public void setAddUserID(Integer addUserID) {
		this.addUserID = addUserID;
	}

	public Integer getRemoveUserID() {
		return removeUserID;
	}

	public void setRemoveUserID(Integer removeUserID) {
		this.removeUserID = removeUserID;
	}

	public Integer getAddModeratorID() {
		return addModeratorID;
	}

	public void setAddModeratorID(Integer addModeratorID) {
		this.addModeratorID = addModeratorID;
	}

	public Integer getRemoveModeratorID() {
		return removeModeratorID;
	}

	public void setRemoveModeratorID(Integer removeModeratorID) {
		this.removeModeratorID = removeModeratorID;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * Method that validates the user input.
	 * 
	 * @param mapping
	 * @param request
	 * @return ActionErrors
	 */
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
		ActionErrors actionErrors = new ActionErrors();

		List<String> errors = new ArrayList<String>();

		Integer userID = (Integer) request.getSession().getAttribute("userID");

		// If the user is not logged in, nothing to do.
		if (userID == null) {
			actionErrors.add("Errors", new ActionMessage("You are not logged in, please do so."));
			errors.add("You are not logged in, please do so.");
		} else {
			Session session = HibernateSessionFactory.getSession();
			session.beginTransaction();

			// Confirms that each user exists in the system.
			if (!addUserIDString.equals("")) {
				try {
					addUserID = Integer.parseInt(addUserIDString);
					User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", addUserID).uniqueResult();
					if (u == null) {
						actionErrors.add("Errors", new ActionMessage("Invalid input for Add User ID.  The user does not exist."));
						errors.add("Invalid input for Add User ID.  The user does not exist.");
					}
				} catch (NumberFormatException e) {
					actionErrors.add("Errors", new ActionMessage("Invalid input for Add User ID.  Input must be an integer."));
					errors.add("Invalid input for Add User ID.  Input must be an integer.");
				}
			}
			if (!removeUserIDString.equals("")) {
				try {
					removeUserID = Integer.parseInt(removeUserIDString);
					User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", removeUserID).uniqueResult();
					if (u == null) {
						actionErrors.add("Errors", new ActionMessage("Invalid input for Remove User ID.  The user does not exist."));
						errors.add("Invalid input for Remove User ID.  The user does not exist.");
					}
				} catch (NumberFormatException e) {
					actionErrors.add("Errors", new ActionMessage("Invalid input for Remove User ID.  Input must be an integer."));
					errors.add("Invalid input for Remove User ID.  Input must be an integer.");
				}
			}
			if (!addModeratorIDString.equals("")) {
				try {
					addModeratorID = Integer.parseInt(addModeratorIDString);
					User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", addModeratorID).uniqueResult();
					if (u == null) {
						actionErrors.add("Errors", new ActionMessage("Invalid input for Add Moderator ID.  The user does not exist."));
						errors.add("Invalid input for Add Moderator ID.  The user does not exist.");
					}
				} catch (NumberFormatException e) {
					actionErrors.add("Errors", new ActionMessage("Invalid input for Add Moderator ID.  Input must be an integer."));
					errors.add("Invalid input for Add Moderator ID.  Input must be an integer.");
				}
			}
			if (!removeModeratorIDString.equals("")) {
				try {
					removeModeratorID = Integer.parseInt(removeModeratorIDString);
					User u = (User) session.createQuery("select u from User u where u.userId = :id").setInteger("id", removeModeratorID).uniqueResult();
					if (u == null) {
						actionErrors.add("Errors", new ActionMessage("Invalid input for Remove Moderator ID.  The user does not exist."));
						errors.add("Invalid input for Remove Moderator ID.  The user does not exist.");
					}
				} catch (NumberFormatException e) {
					actionErrors.add("Errors", new ActionMessage("Invalid input for Remove Moderator ID.  Input must be an integer."));
					errors.add("Invalid input for Remove Moderator ID.  Input must be an integer.");
				}
			}

			session.getTransaction().commit();
			HibernateSessionFactory.closeSession();
		}

		// Store the errors if there are any.
		if (errors.size() > 0) {
			request.setAttribute("Errors", errors);
		}

		return actionErrors;
	}
}